package com.vueapi.controller.admin;

import com.vueapi.annotation.OperationLog;
import com.vueapi.domain.HjCategory;
import com.vueapi.service.HjCategoryService;
import com.vueapi.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Classname CategoryController
 * @Description TODO
 * @Created by 赵宏建
 * @Date 2022/9/21 10:26
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Resource
    private HjCategoryService categoryService;

    @GetMapping("/list")
    public ResultUtils list(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            @RequestParam(required = false, defaultValue = "") String categorySearch) {
        return categoryService.listPage(pageNum, pageSize, categorySearch);
    }

    @OperationLog(name = "")
    @PostMapping("/add")
    public ResultUtils add(@RequestBody HjCategory category) {
        categoryService.save(category);
        return ResultUtils.success("添加成功", null);
    }

    @OperationLog(name = "修改分类")
    @PutMapping("/edit")
    public ResultUtils edit(@RequestBody HjCategory category) {
        categoryService.updateById(category);
        return ResultUtils.success("更新成功", null);
    }
    @OperationLog(name = "删除分类")
    @DeleteMapping("/delete")
    public ResultUtils delete(@RequestParam String ids) {
        List<String> useCategoryIds = categoryService.checkAndRemoveByIds(ids);
        if(useCategoryIds.isEmpty()){
            return ResultUtils.success();
        }
        return ResultUtils.fail(useCategoryIds + "分类正在被使用，不能删除！");
    }

    @OperationLog(name = "修改分类是否置顶")
    @PostMapping("/top")
    public ResultUtils top(@RequestBody HjCategory category) {
        return categoryService.top(category);
    }

    @GetMapping("/getAllCategory")
    public ResultUtils getAllCategory() {
        return ResultUtils.success(categoryService.list());
    }

    @GetMapping("/getCategoryCount")
    public ResultUtils getCategoryCount() {
        return ResultUtils.success(categoryService.getCategoryCount());
    }
}
